﻿using System;
using System.Diagnostics;

namespace FingerTip.Infrastructure.HttpClient
{
    [DebuggerStepThrough]
    public class Api
    {
        private readonly string _token;
        private readonly string _hostUrl;
        private readonly string _version;

        public Api(string hostUrl, string token)
        {
            _hostUrl = hostUrl.EndsWith("/") ? hostUrl.Replace("/$", "") : hostUrl;
            _token = token;
            _version = string.Empty;
        }

        public Api(string hostUrl, string token, string version)
        {
            _hostUrl = hostUrl.EndsWith("/") ? hostUrl.Replace("/$", "") : hostUrl;
            _token= token;
            _version = version;
        }



        public HttpRequestor Get()
        {
            return new HttpRequestor(this, MethodType.Get);
        }

        public HttpRequestor Post()
        {
            return new HttpRequestor(this, MethodType.Post);
        }

        public HttpRequestor Put()
        {
            return new HttpRequestor(this, MethodType.Put);
        }

        public HttpRequestor Delete()
        {
            return new HttpRequestor(this, MethodType.Delete);
        }

        public virtual Uri GetUrl(string tailUrl)
        {
            if (_token != null)
            {
                tailUrl = tailUrl + (tailUrl.IndexOf('?') > 0 ? '&' : '?') + "private_token=" + _token;
            }

            if (_token != null && !_token.StartsWith("/"))
            {
                tailUrl = "/" + tailUrl;
            }
            return new Uri(_hostUrl + _version + tailUrl);
        }
    }
}
